package com.xiaoma.myaudience.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Formatter;
import java.util.Locale;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.text.TextUtils;

/**
 * <br/>字符串工具类
 * @author wjying
 *
 */
public class StringUtils {
    /**
     * <br/>从路径中取得文件名.
     * <br/>比如用于取得图片保存名称
     * @param url
     * @return
     */
    public static String getNameFromPath(String path) {
        if (!TextUtils.isEmpty(path)) {
            return path.substring(path.lastIndexOf("/") + 1);
        }
        return null;
    }
    
    /**
     * <br/>从文件名中取得文件格式.
     * <br/>比如用于取得图片保存格式
     * @param url
     * @return
     */
    public static String getFormatFromName(String url) {
        if (!TextUtils.isEmpty(url)) {
            return url.substring(url.lastIndexOf(".") + 1);
        }
        return null;
    }
    
    /**
     * 格式化时间.
     * 返回 "yyyy-MM-dd HH:mm:ss" 形式
     * @param date
     * @return
     */
    public static String formatTime(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(date);
    }
    
    /**
     * 格式化时间.
     * 返回 "HH:mm:ss" 形式
     * @param date
     * @return
     */
    public static String stringForTime(int timeMs) {
        int totalSeconds = timeMs / 1000;

        int seconds = totalSeconds % 60;
        int minutes = (totalSeconds / 60) % 60;
        int hours   = totalSeconds / 3600;
   
        StringBuilder mFormatBuilder = new StringBuilder();
        Formatter mFormatter = new Formatter(mFormatBuilder, Locale.getDefault());
        mFormatBuilder.setLength(0);
        if (hours > 0) {
            return mFormatter.format("%d:%02d:%02d", hours, minutes, seconds).toString();
        } else {
            return mFormatter.format("%02d:%02d", minutes, seconds).toString();
        }
    }
    
    /**
     * 从 "HH:mm:ss" 形式返回 millionsecond
     * @param time
     * @return
     */
    public static int timeForString(String time) {
    	String[] times = time.split(":");
    	int size = times.length; 
    	int seconds = 0;
    	try {
			for(int i = size -1; i>=0; i--) {
				seconds += (int) ((Math.pow(60, size-i-1))* (Integer.parseInt(times[i])));
			}
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        Logger.i("timeForString "+seconds);
		return seconds*1000;
    }
    
    /**
     * 拼字符串
     * @param context
     * @param resourceid 字符串的资源id
     * @param s
     * @return
     */
    public static String appendString(Context context, int resourceid, String s) {
    	StringBuilder builder = new StringBuilder();
    	builder.append(context.getString(resourceid)).append(s);
    	return builder.toString();
    }
    
    public static String getImagePath(Context context, Uri uri) {
        String imagePath = null;
        if (uri == null) {
            return null;
        }
        String type = uri.getScheme();
        if (type.equals(ContentResolver.SCHEME_CONTENT)) {
            String[] filePathColumn = { MediaStore.Images.Media.DATA };
            Cursor cursor = context.getContentResolver().query(uri, filePathColumn, null, null, null);
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            imagePath = cursor.getString(columnIndex);
            cursor.close();
        } else {
            imagePath = uri.getPath();
        }
        return imagePath;

    }
    
}
